package com.bjc.sdx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bjc.sdx.R;
import com.bjc.sdx.adapter.CustomerAdapter;
import com.bjc.sdx.adapter.MyGridAdapter;
import com.bjc.sdx.adapter.MyPagerAdapter;
import com.bjc.sdx.model.ChildObj;
import com.bjc.sdx.model.Fruit;
import com.bjc.sdx.model.ParentObj;
import java.util.ArrayList;
import java.util.List;

public class DrawerActivity extends AppCompatActivity {
    private LayoutInflater mInflater;
    private ArrayList<Fruit> mList;
    private static final String TAG = "DrawerActivity";
    private ExpandableListView expandableListView;
    private CustomerAdapter customerAdapter;
    private ArrayList<ParentObj> listData = new ArrayList<ParentObj>();

    private View view1, view2, view3, view4;
    private ViewPager viewPager;  //对应的viewPager
    private List<View> viewList;//view数组
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_main);

        /*--------------------侧边栏的数据及View--------------------------*/
        createGroupData();

        createChildData();

        expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view_id);
        customerAdapter = new CustomerAdapter(listData, this);
        expandableListView.setAdapter(customerAdapter);
        expandableListView.setGroupIndicator(null);

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0; i < customerAdapter.getGroupCount(); i ++) {
                    if (i != groupPosition) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(DrawerActivity.this, listData.get(groupPosition).getChilds().get(childPosition).getcName(), Toast.LENGTH_LONG).show();
                return true;
            }
        });
        /*----------------------------------------------*/


        /*--------------------主界面的内容--------------------------*/
        mInflater = getLayoutInflater();
        mList = new ArrayList<Fruit>();
        for (int i = 0; i <= 4; i++) {
            Fruit apple = new Fruit("4K", R.drawable.apple);
            Fruit banana = new Fruit("5G", R.drawable.banana);
            Fruit grape = new Fruit("通讯录", R.drawable.grape);
            Fruit strawberry = new Fruit("休眠", R.drawable.strawberry);
            Fruit baiduCloud = new Fruit("百度云盘", R.drawable.baidu_cloud);
            mList.add(apple);
            mList.add(banana);
            mList.add(grape);
            mList.add(strawberry);
            mList.add(baiduCloud);
        }

        MyGridAdapter adapter = new MyGridAdapter(mInflater, mList);

        // 分页功能
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        LayoutInflater inflater = getLayoutInflater();

        view1 = inflater.inflate(R.layout.gridview_layout, null);
        GridView gridView1 = view1.findViewById(R.id.gridView1);
        gridView1.setAdapter(adapter);
        // 点击事件
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(DrawerActivity.this, "你点击的应用为：" +
                        mList.get(position).getName() + ",  id = : " + id + ", position = " + position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                if(position % 5 == 0 ) {
                    intent.setClass(DrawerActivity.this, DrawerActivity2.class);
                    DrawerActivity.this.startActivity(intent);
                } else if(position % 5 == 1 ){
                    intent.setClass(DrawerActivity.this, ExpandActivity.class);
                    DrawerActivity.this.startActivity(intent);
                } else if(position % 5 == 2 ){
                    intent.setClass(DrawerActivity.this, GridViewActivity.class);
                    DrawerActivity.this.startActivity(intent);
                }else if(position % 5 == 3 ){
                    intent.setClass(DrawerActivity.this, ViewPagerActivity.class);
                    DrawerActivity.this.startActivity(intent);
                }else if(position % 5 == 4 ){
                    intent.setClass(DrawerActivity.this, ViewPagerActivity2.class);
                    DrawerActivity.this.startActivity(intent);
                }
            }
        });

        button = view1.findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(DrawerActivity.this, GridViewActivity.class);
                DrawerActivity.this.startActivity(intent);
            }
        });

        button = view1.findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(DrawerActivity.this, ViewPagerActivity2.class);
                DrawerActivity.this.startActivity(intent);
            }
        });

        view2 = inflater.inflate(R.layout.viewpager_layout1, null);
        view3 = inflater.inflate(R.layout.viewpager_layout2, null);
        view4 = inflater.inflate(R.layout.viewpager_layout3, null);
        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);
        PagerAdapter pagerAdapter = new MyPagerAdapter(viewList);
        viewPager.setAdapter(pagerAdapter);


        final DrawerLayout drawerLayout = findViewById(R.id.drawerlayout);
        //给按钮添加一个监听器
        findViewById(R.id.top_view_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 这里设置的方向要与layout里面的保持一致android:layout_gravity="end"
                //打开侧滑菜单
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                Log.w(TAG, "onDrawerSlide 侧边栏开始滑动");
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerOpened  打开侧边栏");
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                Log.w(TAG, "onDrawerClosed 关闭侧边栏");
            }


            @Override
            public void onDrawerStateChanged(int newState) {
                Log.w(TAG, "onDrawerStateChanged 侧边栏状态发生改变 newState=" + newState);
            }
        });

    }

    /*----------------------------------------------*/



    private void createGroupData() {
        ParentObj p1 = new ParentObj();
        p1.setpName("HP");
        listData.add(p1);

        ParentObj p2 = new ParentObj();
        p2.setpName("DELL");
        listData.add(p2);

        ParentObj p3 = new ParentObj();
        p3.setpName("Lenovo");
        listData.add(p3);

        ParentObj p4 = new ParentObj();
        p4.setpName("Sony");
        listData.add(p4);

        ParentObj p5 = new ParentObj();
        p5.setpName("HCL");
        listData.add(p5);

        ParentObj p6 = new ParentObj();
        p6.setpName("Samsung");
        listData.add(p6);
    }

    private void createChildData() {
        // preparing laptops collection(child)
        String[] hpModels = { "HP Pavilion G6-2014TX", "ProBook HP 4540", "HP Envy 4-1025TX" };
        String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
        String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series", "ThinkPad X Series", "Ideapad Z Series" };
        String[] sonyModels = { "VAIO E Series", "VAIO Z Series", "VAIO S Series", "VAIO YB Series" };
        String[] dellModels = { "Inspiron", "Vostro", "XPS" };
        String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

        for (ParentObj p : listData) {
            if (p.getpName().equals("HP")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < hpModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(hpModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("DELL")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < dellModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(dellModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("Lenovo")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < lenovoModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(lenovoModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("Sony")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < sonyModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(sonyModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else if (p.getpName().equals("HCL")) {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < hclModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(hclModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            } else {
                ArrayList<ChildObj> clists = new ArrayList<ChildObj>();
                for (int i = 0; i < samsungModels.length; i ++) {
                    ChildObj cObj = new ChildObj();
                    cObj.setcName(samsungModels[i]);
                    clists.add(cObj);
                }
                p.setChilds(clists);
            }
        }
    }
}
